﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Dynamic;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Lib.Paging;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Linq;
using SDT.Lib.Helpers;

namespace SDT.Admin.Controllers
{
    /// <summary>
    /// Author: Takamaru Nguyen
    /// Date: 30/06/2011
    /// </summary>'
	[RoleHelper(Roles = RoleSystem.Product)]
    public class ProductAttributeController : Controller
    {
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        //
        // GET: /ProductAttribute/
        public ActionResult Index(int? page)
        {
            try
            {
                    if (page == null)
                        page = 1;
                    string order = UserState.getUserStateFromRequest("productAttribute.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("productAttribute.orderDir", "orderDir", "desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("productAttribute.Keyword", "Keyword", "").ToString();
                    int pageSize = int.Parse(UserState.getUserStateFromRequest("productAttribute.pageSize", "PageSize", 20).ToString());

                    var pros = eDataContext.ProductAttributes
                        .Where(p => (string.IsNullOrEmpty(keyword) ? true :
                            (p.Name.ToLower().Contains(keyword.ToLower().Trim()) ||
                            p.Name.ToLower().Contains(keyword.ToLower().Trim()) ||
                            p.Name.ToLower().Contains(keyword.ToLower().Trim())
                        )))
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(page.Value - 1, pageSize);

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;

                    return View(new ProductAttributeViewModels
                    {
                        ProductAttributes = pros,
                        Keyword = keyword,
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductAttribute/Details/5
        public ActionResult Details(int id)
        {
            try
            {
                    ProductAttribute proAtt = eDataContext.ProductAttributes.Where(pa => pa.ID == id).FirstOrDefault();
                    return View(new ProductAttributeInputModels
                        {
                            ProductAttributes = proAtt
                        });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductAttribute/Create
        public ActionResult Create()
        {
            try
            {
                    return View(new ProductAttributeInputModels());
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /ProductAttribute/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                    ProductAttribute proAtt = new ProductAttribute();
                    UpdateModel(proAtt);
                    eDataContext.ProductAttributes.InsertOnSubmit(proAtt);
                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductAttribute/Edit/5
        public ActionResult Edit(int id)
        {
            try
            {
                    ProductAttribute proAtt = eDataContext.ProductAttributes.Where(pa => pa.ID == id).FirstOrDefault();
                    return View(new ProductAttributeInputModels
                    {
                        ProductAttributes = proAtt,
                        Types = 1
                    });
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /ProductAttribute/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                    ProductAttribute proAtt = eDataContext.ProductAttributes.Where(pa => pa.ID == id).FirstOrDefault();
                    UpdateModel(proAtt);

                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductAttribute/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                    ProductAttribute pro = eDataContext.ProductAttributes.SingleOrDefault(e => e.ID == id);

                    return View(pro);
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /ProductAttribute/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                    ProductAttribute pro = eDataContext.ProductAttributes.SingleOrDefault(e => e.ID == id);
                    eDataContext.ProductAttributes.DeleteOnSubmit(pro);
                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
    }
}
